#include "SDLProgressIndicator.h"

using namespace sdl;

SDLProgressIndicator::SDLProgressIndicator(int xpos, int ypos, SDLSurface* surface) : x(xpos), y(ypos), sdlSurface(surface)
{
	monitor = 0;
	image = 0;
	name = "";
	initializeFont();
}

SDLProgressIndicator::~SDLProgressIndicator(void)
{
	delete monitor;
	delete image;
}

void SDLProgressIndicator::initializeFont(void)
{
	font = TTF_OpenFont("..\\fonts\\arial.ttf", 8);
	if (!font)
	{
		string message("No es posible abrir el font arial.ttf. El error reportado por SDL es: '");
		message.append(TTF_GetError()).append("'.");
		Logger::instance().log(message);
	}
}

void SDLProgressIndicator::setMonitor(SDLProgressMonitor* pm)
{
	monitor = pm;
}

void SDLProgressIndicator::setImage(SDLSurface* image)
{
	this->image = image;
}

void SDLProgressIndicator::setName(string name)
{
	this->name = name;
}

void SDLProgressIndicator::draw(void)
{
	bool showIndicator = false;
	if (monitor)
	{
		if (monitor->getProgress() > 0)
		{
			showIndicator = true;
		}
	}
	if (showIndicator && font && name.size() > 0)
	{
		TTF_SetFontStyle(font, 0);
		SDL_Color color = {255, 0, 0};
		SDL_Surface* textSurface = TTF_RenderUTF8_Blended(font, name.c_str(), color) ;
		if (textSurface != NULL)
		{
			SDL_Rect source;
			source.x = 0;
			source.y = 0;
			source.w = sdlSurface->getWidth();
			source.h = sdlSurface->getHeight();

			SDL_Rect destination;
			destination.x = x;
			destination.y = y;
			destination.w = sdlSurface->getWidth();
			destination.h = sdlSurface->getHeight();
			SDL_BlitSurface(textSurface, &source, sdlSurface->getSurface(), &destination);
			SDL_FreeSurface(textSurface);
		}
	}
	if (showIndicator && image)
	{ 
		sdlSurface->draw(image, x, y+10);
	}
}
